<?php

namespace app\admin\controller;
use think\Db;

class Order
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
        $where = [];
        $order_id = input('get.order_id');
        $start_date = request()->get('start_date');
        $stop_date = request()->get('stop_date');
        if (!empty($order_id)) {
            $where['order_id'] = $order_id;
        }
        if(!empty($start_date) && !empty($stop_date)){
            $where['date'] = ['between',$start_date.','.$stop_date];
        }
        $pages = pages('order', $where, input('get.page'));
        $data = Db::table("order")->where($where)->whereIn('pid', session('wmo2o_a_pid'))->limit($pages['limit'])->order('time desc')->select();
        return view('', ['data' => $data, 'pages' => $pages['pages']]);
    }

    public function fahuo()
    {
        $id = input("get.id");
        $fahuo = input("get.fahuo");
        if (!$id || !$fahuo) {
            return json(array("code" => 0, "msg" => "数据错误，请刷新页面重试！"));
        }
        $order = Db::table("order")->where(['id' => $id, 'status' => 1])->find();
        if (count($order) == 0) {
            return json(array("code" => 0, "msg" => "订单不存在或已发货！"));
        } else {
            if (Db::table("order")->where(['id' => $id, 'status' => 1])->update(['status' => 2, "fahuo" => $fahuo])) {

                $goods_array = Db::table('order_goods')->where('order_id',$order['order_id'])->select();
                foreach ($goods_array as $k => $v){
                    $goods = Db::table('service')->where('id',$v['gid'])->find();
                    $spec = json_decode($goods['spec']);
                    $info = [];
                    foreach ($spec as $k2 => $v2){
                        $info[$k2]['title'] = $v2->title;
                        $info[$k2]['price0'] = $v2->price0;
                        $info[$k2]['price1'] = $v2->price1;
                        $info[$k2]['price2'] = $v2->price2;
                        if($v['spc'] == $v2->title){
                            $info[$k2]['stock'] = (int)$v2->stock-(int)$v['number'];
                        }else{
                            $info[$k2]['stock'] = $v2->stock;
                        }
                    }
                    $spec = json_encode($info);
                    Db::table('service')->where('id',$v['gid'])->update(['spec'=>$spec]);
                    Db::table('service')->where('id',$v['gid'])->setInc('volume');
                }
                push(md5($order['uid']),json_encode(['type'=>0,'message'=>'您的订单已发货：订单编号为'.$fahuo,'parameters'=>$order['order_id'],'status'=>5]));
                insert_message($order['id'],'订单提醒','您的订单已发货：订单编号为'.$fahuo,5,$order['order_id']);
                return json(array("code" => 1, "msg" => "发货成功！"));
            }
        }
    }

    public function wancheng()
    {
        $id = input("get.id");
        if (!$id) {
            return json(array("code" => 0, "msg" => "数据错误，请刷新页面重试！"));
        }
        $order = Db::table("order")->where(['id' => $id, 'status' => 2])->find();
        if (!$order) {
            return json(array("code" => 0, "msg" => "订单不存在或已完成！"));
        } else {
            if (Db::table("order")->where(['id' => $id, 'status' => 2])->update(['status' => 3])) {
                return json(array("code" => 1, "msg" => "操作成功！"));
            }
        }
    }

    public function ispay()
    {
        $id = input("get.id");
        $is_pay = input("get.is_pay");
        if (!$id) {
            return json(array("code" => 0, "msg" => "数据错误，请刷新页面重试！"));
        }
        $order = Db::table("order")->where(['id' => $id])->value("id");
        if (!$order) {
            return json(array("code" => 0, "msg" => "订单不存在！"));
        } else {
            if (Db::table("order")->where(['id' => $id])->update(['ispay' => $is_pay])) {
                return json(array("code" => 1, "msg" => "操作成功！"));
            }
        }
    }

    public function del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $page = input("get.page");
        Db::table("order")->where('id',$id)->whereIn('pid', session('wmo2o_a_pid'))->delete();
        redirect(url('order/index') . '?page=' . $page);
        exit;
    }

    public function detail()
    {
        $order_id = input("get.order_id")?input("get.order_id"):0;
        $data = Db::table('order_goods')->where('order_id',$order_id)->select();
        foreach ($data as $k => $v){
            $goods = Db::table('service')->where('id',$v['gid'])->find();
            $data[$k]['title'] = $goods['title'];
            $data[$k]['image'] = $goods['image1'];
        }
        $info = Db::table('order')->where('order_id',$order_id)->find();
        $info['data'] = $data;
        return view('',['info'=>$info]);
    }
    
    public function examine()
    {
        $pages = pages('user', ['type' => 1, 'status' => 2], input('get.page'));
        $data = Db::table("user")->where(['type' => 1, 'status' => 2])->limit($pages['limit'])->order('id desc')->select();
        return view('', ['data' => $data, 'pages' => $pages['pages']]);
    }


    public function setting()
    {
        $tab = input("get.tab");
        if (request()->isPost()) {
            $data = input("post.");
            Db::table("payment")->where(['id' => 1])->update($data);
        }
        $data = Db::table("payment")->find();
        return view('', ['data' => $data, 'tab' => $tab]);
    }
}
